package com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.utils;


import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.constant.CommonConstant;
import com.yunweiweishi.xuhuanpaltform.yuhuan_app.comm.fina.TextUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xiaofan
 * @date 2018/12/14 11:10
 */
public class CommonUtil {
    /**
     * Long型时间戳转日期格式
     *
     * @param timestamp
     * @return
     */
    public static Date timeStampToDate(Long timestamp) {
        return new Date(timestamp);
    }

    /**
     * 逗号分隔字符串转换成Long类型数组
     *
     * @param s
     * @return
     */
    public static List<Long> splitStringToLongList(String s) {
        List<Long> ret = new ArrayList<>();
        if (s != null && s.length() > 0) {
            String[] split = s.split(",");
            for (String s1 : split) {
                try {
                    ret.add(Long.valueOf(s1));
                } catch (NumberFormatException e) {
                    return new ArrayList<>();
                }
            }
        }
        return ret;
    }

    /**
     * 校验密码是否合法 长度 6 - 20个字符
     *
     * @param password
     * @return
     */
    public static boolean isPasswordLegal(String password) {
        if (password == null || password.length() < CommonConstant.PASSWORD_MIN_LENGTH || password.length() > CommonConstant.PASSWORD_MAX_LENGTH) {
            return false;
        }
        return true;
    }

    /**
     * 校验用户姓名是否合法 长度1-10个字符
     * @param name
     * @return
     */
    public static boolean isNameLegal(String name){
        if (name == null || name.length() < CommonConstant.USERNAME_MIN_LENGTH || name.length() > CommonConstant.USERNAME_MAX_LENGTH) {
            return false;
        }
        return true;
    }

    // 可以根据自己需求来删减下面的代码，不要要的类型可以删掉
    // type=0：纯数字(0-9)
    // type=1：全小写字母(a-z)
    // type=2：全大写字母(A-Z)
    // type=3: 数字+小写字母
    // type=4: 数字+大写字母
    // type=5：大写字母+小写字母
    // type=6：数字+大写字母+小写字母
    // type=7：固定长度33位：根据UUID拿到的随机字符串，去掉了四个"-"(相当于长度33位的小写字母加数字)

    /**
     * @param passLength : 要生成多少长度的字符串
     * @param type       : 需要哪种类型
     * @return 根据传入的type来判定
     */
    public static String getRandomCode(int passLength, int type) {
        StringBuffer buffer = null;
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        r.setSeed(System.currentTimeMillis());
        switch (type) {
            case 0:
                buffer = new StringBuffer("0123456789");
                break;
            case 1:
                buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
                break;
            case 2:
                buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                break;
            case 3:
                buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyz");
                break;
            case 4:
                buffer = new StringBuffer("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                break;
            case 5:
                buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
                break;
            case 6:
                buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
                sb.append(buffer.charAt(r.nextInt(buffer.length() - 10)));
                passLength -= 1;
                break;
            case 7:
                String s = UUID.randomUUID().toString();
                sb.append(s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24));
            default:
                buffer = new StringBuffer("0123456789");
                break;
        }

        if (type != 7) {
            int range = buffer.length();
            for (int i = 0; i < passLength; ++i) {
                sb.append(buffer.charAt(r.nextInt(range)));
            }
        }
        return sb.toString();
    }

    /**
     * Long数字列表转换成逗号分隔字符串
     *
     * @param list
     * @return
     */
    public static String listToString(List<Long> list) {
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1) {
                stringBuffer.append(list.get(i).toString()).append(",");
            } else {
                stringBuffer.append(list.get(i));
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 获取交易号
     *
     * @return
     */
    public static String genTradeNum() {
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + getRandomCode(3, 0);
    }

    public static void main(String[] args) {
        List<Long> list = new ArrayList<>();
        list.add(1L);
        list.add(1L);
        list.add(1L);
        list.add(1L);
        list.add(1L);
        System.out.println(listToString(list));
    }

    /**
     * 计算两个时间的差
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        String h, m, s;
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
//        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        if (hour < 10) {
            h = "0" + hour;
        } else {
            h = "" + hour;
        }
        if (min < 10) {
            m = "0" + min;
        } else {
            m = "" + min;
        }
        if (sec < 10) {
            s = "0" + sec;
        } else {
            s = "" + sec;
        }
        return h + ":" + m + ":" + s;
    }

    /**
     * 验证手机号
     *
     * @param mobileNums
     * @return
     */
    public static boolean isMobile(String mobileNums) {
        /**
         * 判断字符串是否符合手机号码格式
         * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
         * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
         * 电信号段: 133,149,153,170,173,177,180,181,189
         * @param str
         * @return 待检测的字符串
         */
        // "[1]"代表下一位为数字可以是几，"[0-9]"代表可以为0-9中的一个，"[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\\d{9}"代表后面是可以是0～9的数字，有9位。
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";
        if (TextUtils.isEmpty(mobileNums)) {
            return false;
        } else {
            return mobileNums.matches(telRegex);
        }
    }

}
